package com.example.demo.Service;

import com.example.demo.model.Comment;
import com.example.demo.model.Movie;
import com.example.demo.repository.CommentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class CommentService {

    @Autowired
    private CommentRepository commentRepository;

    @Autowired
    private MovieService movieService;

    public List<Comment> getCommentsByMovieId(Long movieId) {
        return commentRepository.findByMovieId(movieId);
    }

    public Comment addCommentToMovie(Long movieId, Comment comment) {
        Optional<Movie> optionalMovie = movieService.getMovieById(movieId);
        if (optionalMovie.isPresent()) {
            Movie movie = optionalMovie.get();
            comment.setMovie(movie);
            return commentRepository.save(comment);
        } else {
            throw new RuntimeException("Movie not found with id: " + movieId);
        }
    }

    public Comment updateComment(Comment comment) {
        return commentRepository.save(comment);
    }

    public void deleteComment(Long id) {
        commentRepository.deleteById(id);
    }
}